
var g_sKey = '';
var g_iCategoryID = 0;

var g_iTotal = 0;
var g_iPageNo = 0;
var g_iMaxPageNo = 0;
var g_iOffset = 0;
var g_iLimit = 30;


$(function(){

	$("#form-search").validate({
		rules: {
			key: {
				required: true
			}
		},
		messages: {
			key: {
				required: ""
			}
		},
		submitHandler: function(form){
			
			var eSubmit = document.getElementById("submit");
			eSubmit.disabled = true;
			
			g_sKey = $("#key").val();
			g_iCategoryID =  $("#category_id").val();

			$("#technics").html(g_sLoadingImage + "搜索中...");
			
			$.ajax({
				type: "POST",
				url: "./?controller=technic&task=ajax_search",
				data: $(form).serialize(),
				dataType: "json",
				success: function(json){
					if(json.status=="0")
					{
						g_iTotal = Number(json.total);
						if(g_iTotal>0)
						{
							if(g_iTotal%g_iLimit == 0 ) g_iMaxPageNo = g_iTotal/g_iLimit;
							else g_iMaxPageNo = Math.floor( g_iTotal/g_iLimit ) + 1;
							gotoPage(1);
						}
						else
						{
							$("#technics").html("没有搜索到合适的记录");
							$("#pagination").html("");
						}
					}
					else
					{
						alert(json.description);
					}
					
					eSubmit.disabled = false;
				}
			});
		}
	});
});


function gotoPage(iPageNo)
{
	if(iPageNo>0 && iPageNo<=g_iMaxPageNo)
		g_iPageNo = iPageNo;
	else
		return;
	g_iOffset = (g_iPageNo-1)*g_iLimit;
	
	updatePagination();

	$("#technics").html(g_sLoadingImage + "正在获取数据...");
	
	$.ajax({
		type: "POST",
		url: "./?controller=technic&task=ajax_search_data",
		data: {
			"key":g_sKey,
			"category_id":g_iCategoryID,
			"limit":g_iLimit,
			"offset":g_iOffset
		},
		dataType: "json",
		success: function(json){
			$("#technics").html("");
			if(json.status=="0")
			{
				var sModel, oTechnic;
				var arrTechnic = json.technics;
				for(var i=0, n=arrTechnic.length; i<n; i++)
				{
					sModel  = '<div class="technic row-'+(i%2)+'">';
					sModel += '<ul><li class="name">[{category_name}] <a href="javascript:;" onclick="javascript:popup(\'{url}\');">{title}</a></li><li class="create_time">{create_time}</li></ul>';
					sModel += '<div class="clrl"></div>';
					sModel += '</div>';
					oTechnic = arrTechnic[i];
					for(var x in oTechnic)
					{
						var reg=new RegExp("{"+x+"}","g");
						sModel = sModel.replace(reg, oTechnic[x]);
					}
					$("#technics").append(sModel);
				}
			}
		}
	});
}


function popup(url)
{
	var iWidth = 600;
	var iHeight = 500;
	var iWindowsWidth  = jQuery(window).width();
	var iWindowsHeight = jQuery(window).height();
	
	var iLeft = (iWindowsWidth-iWidth)>>1;
	var iTop = (iWindowsHeight-iHeight)>>1;

	window.open(url, 'popwindow', 'width='+iWidth+',height='+iHeight+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,location=no,status=no') 	
}


function updatePagination()
{
	var s = '';	
	if(g_iMaxPageNo>1)
	{		
		s += '<div class="pagination">';
		if(g_iPageNo==1)
		{
			s += '<span class="first">&lt;&lt;</span>';
			s += '<span class="pre">&lt;</span>';
		}
		else
		{
			s += '<span class="first"><a href="javascript:gotoPage(1)">&lt;&lt;</a></span>';
			s += '<span class="pre"><a href="javascript:gotoPage('+(g_iPageNo-1)+')">&lt;</a></span>';
		}

		s += '<span class="current">'+g_iPageNo+'/'+g_iMaxPageNo+'</span>';

		if(g_iPageNo < g_iMaxPageNo)
		{
			s += '<span class="next"><a href="javascript:gotoPage('+(g_iPageNo+1)+')">&gt;</a></span>';
			s += '<span class="last"><a href="javascript:gotoPage('+ g_iMaxPageNo +')">&gt;&gt;</a></span>';
		}
		else
		{
			s += '<span class="next">&gt;</span>';
			s += '<span class="last">&gt;&gt;</span>';
		}
		
		s += '<span class="goto">';
		s += '<select onchange="javascript:gotoPage(this.options[this.selectedIndex].value);">';
		for(var i=1; i<=g_iMaxPageNo; i++)
		{
			s += '<option value="'+i+'"';
			if(i == g_iPageNo) s += ' selected';
			s += '>'+i+'</option>';
		}
		s += '</select>';
		s += '</span>';
		s += '</div>';
	}
	$("#pagination").html(s);
}

